function res = ITCGloop(resp)
global hInt
t = resp(1) + hInt;
statep = resp(2:7);
tPNGp = resp(10);
alphad = resp(11);
bankd = resp(12);
etp = resp(13);
x = statep(1);
y = statep(2);
z = statep(3);
fpa = statep(5);
azi = statep(6);
SMT = sqrt(x^2+y^2);

lambda = atan(z/SMT);
if x >= 0
    phi = asin(y/SMT);
else
    phi = -(asin(y/SMT)+pi);
end
delta = fpa + lambda;
eta = azi + phi + pi/2;

state = rk4(@TerminalDynamic, statep, alphad, bankd, hInt);
[r, dr] = calRandDR(state);
res(1) = t;
res(2:7) = state;
res(8) = lambda;
res(9) = phi;
res(10) = tPNGp;
res(11) = alphad;
res(12) = bankd;
res(13) = etp;
res(14) = r;
res(15) = dr;
res(16) = delta;
res(17) = eta;
end